package com.ro.common.util;

import com.google.common.base.Splitter;
import com.jfinal.plugin.redis.Redis;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Util {

    /**
     * 按逗号截取
     *
     * @param string
     * @return
     */
    public List<String> utilSplit(String string) {
        return Splitter.on(",").splitToList(string);
    }

    /**
     * 字符串截取
     *
     * @param str
     * @return
     */
    public String abbrStr(String str, int length) {
        if (str != null && !"".equals(str)) {
            if (str.length() > length) {
                return str.substring(0, length);
            }
        }
        return str;
    }

    public boolean isLoginState() {
        boolean authFlag;
        try {
            authFlag = Redis.use("ec").get("auth");
        } catch (Exception e) {
            return false;
        }
        return authFlag;
    }

    public String isLoginTime() {
        StringBuffer stringBuffer = new StringBuffer();
        try {
            long thisTime = Redis.use("ec").get("thisTime");
            long authTime = (int) Redis.use("ec").get("authTime");
            stringBuffer.append("登录生效时常时长: <span style='color:red;'>").append(authTime / 60).append("</span> 分钟 ");
            long time = (thisTime + authTime);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date = new Date((time * 1000));
            stringBuffer.append(" 本次登录生效至: <span style='color:red;'>").append(sdf.format(date)).append("</span>");
        } catch (Exception e) {
            return e.getMessage();
        }
        return stringBuffer.toString();
    }


}
